package com.yishun.springboot.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yinquan.util.DataUtil;
import com.yinquan.util.ParseYzwResult;
import com.yishun.springboot.pojo.YzwProjectAttendanceListNew;

import lombok.extern.slf4j.Slf4j;

/**
 * （工务署项目的）直连云筑网获取数据服务类
 * 
 * @author xiaohuan
 * @date 2022年4月2日 下午5:14:44
 */
@Slf4j
@Service
public class DirectlyConnectedYzwPersonService {

	// 是否详细记录
	static Boolean detailedRecordLong = false;

	@Autowired
	RealNameSystemInterfaceService realNameSystemInterfaceService;

	public List<YzwProjectAttendanceListNew> getAtt(Integer yzwProjectCode, String yzwProjectKey, String workerName,
			String beginDate, String endDate) {
		JSONObject headerJsonObject = new JSONObject(); // 通用装header属性的map 到发送请求类进行set
		JSONObject jsonObject = new JSONObject(); // 请求体
		String tempVariable = "";
		ParseYzwResult parseYzwResult = new ParseYzwResult();
		List<YzwProjectAttendanceListNew> yzAl = null;
		if (detailedRecordLong)
			log.info("工务署业务模块-上传考勤到工务署-开始获取" + workerName + "的直连云筑网考勤数据...");

		int maxReqInt = 13;
		int cycleInt = 1;

		String todayZero = DataUtil.todayZero();
		for (int j = 0; j < cycleInt; j++) {

			// 如果没有找到就直连云筑网查找有没有考勤，没有就直接跳过该员工
			headerJsonObject.put("projectKey", yzwProjectKey);
			headerJsonObject.put("projectCode", yzwProjectCode);
			jsonObject.put("ProjectCode", yzwProjectCode);
			jsonObject.put("WorkerName", workerName);
			jsonObject.put("BeginDate", todayZero);
			jsonObject.put("EndDate", DataUtil.currTime());

			// 解析从云筑网返回的数据，一步步解析，如果为空就跳过该员工
			try {
				tempVariable = realNameSystemInterfaceService.GetProjectAttendanceListNew(jsonObject, headerJsonObject);
			} catch (Exception e) {
				log.error("(工务署业务模块-上传考勤到工务署-获取本机要上传的项目人员完成,数据为空，即将结束该次会话)\n" + e);
				log.info("工务署业务模块-上传考勤到工务署-获取本机要上传的项目人员完成,数据为空，即将结束该次会话");

			}
			tempVariable = parseYzwResult.parseYzwCode(tempVariable);
			if (tempVariable == null) {
				log.error("工务署业务模块-上传考勤到工务署-开始获取云筑网考勤数据请求失败...即将重新进行请求");
				cycleInt = 1;
			}
			maxReqInt++;
			if (maxReqInt == 13) {
				if (detailedRecordLong)
					log.error("工务署业务模块-上传考勤到工务署-开始获取云筑网考勤数据请求失败...超过最大请求次数13");
				return null;
			}

			if (tempVariable == null)
				continue;
			tempVariable = parseYzwResult.parseYzwInData(tempVariable);
			if (tempVariable == null || tempVariable.equals("0"))
				continue;
			yzAl = parseYzwResult.parseArrayToAtt(tempVariable);
			if (yzAl.size() == 0) {
				continue;
			}

			log.info("工务署业务模块-上传考勤到工务署-获取云筑网考勤数据完！");
		}
		return yzAl;
	}
}
